home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 127_01 / rap3.c < prev    next >
Text File  |  1985-03-09  |  14KB  |  553 lines

  1. /*********************************************************************\
  2. ** .---------------------------------------------------------------. **
  3. ** |                                                               | **
  4. ** |                                                               | **
  5. ** |         Copyright (c) 1981, 1982, 1983 by Eric Martz.         | **
  6. ** |                                                               | **
  7. ** |                                                               | **
  8. ** |       Permission is hereby granted to use this source         | **
  9. ** |       code only for non-profit purposes. Publication of       | **
  10. ** |       all or any part of this source code, as well as         | **
  11. ** |       use for business purposes is forbidden without          | **
  12. ** |       written permission of the author and copyright          | **
  13. ** |       holder:                                                 | **
  14. ** |                                                               | **
  15. ** |                          Eric Martz                           | **
  16. ** |                         POWER  TOOLS                          | **
  17. ** |                    48 Hunter's Hill Circle                    | **
  18. ** |                      Amherst MA 01002 USA                     | **
  19. ** |                                                               | **
  20. ** |                                                               | **
  21. ** `---------------------------------------------------------------' **
  22. \*********************************************************************/
  23.  
  24. #include "rap.h"
  25.  
  26. /*-------------------------------------------------------------------------*/
  27. getwrd(next)
  28.  
  29. /* STARTING AT THE POINTER *next, FINDS THE NEXT WORD AND SETS THE POINTERS
  30. Wordbegin AND Wordend, MOVING *next TO THE CHARACTER AFTER Wordend. Wordlen
  31. IS CALCULATED TAKING INTO ACCOUNT IMBEDDED POST_SS COMMANDS OR BACKSPACES.
  32. RETURNS Wordlen WHICH IS ZERO IF LAST WORD WAS GOTTEN ON PREVIOUS CALL. IF
  33. AN OPENING POST_SS_DELIMITER IS FOUND IN THE WORD, THE WORD MUST NOT END
  34. UNTIL A CLOSING DELIMITER IS FOUND, TO PREVENT BREAKING A LINE WITHIN AN
  35. ORIGINAL STRING TO BE POST-SUBSTITUTED. */
  36.  
  37.     char **next;
  38.     {
  39.     char *p1, *p2;
  40.     int even_delim;
  41.     if (**next EQ NULL) return(0);
  42.     even_delim = YES;
  43.  
  44.     /* FIND WORD */
  45.     if (Post_ss) {
  46.         for(p1 = *next;
  47.             ((*p1 NE NULL) AND
  48.             ((even_delim EQ NO) OR
  49.             (*p1 NE SPACE AND *p1 NE TAB AND *p1 NE DASH)))
  50.             ;p1++) {
  51.                 if(*p1 EQ Post_ss_delim) {
  52.                     if (even_delim) even_delim = NO;
  53.                     else even_delim = YES;
  54.                 }
  55.         }
  56.     }
  57.     else {
  58.         for(p1 = *next;
  59.             ((*p1 NE NULL) AND
  60.             (*p1 NE SPACE AND *p1 NE TAB AND *p1 NE DASH))
  61.             ;p1++)
  62.                 ;
  63.     }
  64.     
  65.  
  66.     if ((*p1 EQ NULL) AND (even_delim EQ NO)) {
  67.         fprintf(STDERR,
  68.             "Fatal error in line %d:\n%s%s",
  69.             In_linecnt,
  70.             "opening post-formatting string substitution delimiter\n",
  71.             "without closing delimiter.\n");
  72.         exit(0);
  73.     }
  74.  
  75.     /* ALLOW BREAK AT DASH BUT INCLUDE DASH IN WORD */
  76.     if (*p1 EQ DASH) p1++;
  77.  
  78.     /* SET EXTERNALS */
  79.     Wordbegin = *next;
  80.     Wordend = p1 - 1;
  81.     Wordlen = p1 - *next;
  82.     *next = p1;
  83.  
  84.     /* Wordlen CORRECTION ROUTINE */
  85.     for (p1 = Wordbegin; p1 <= Wordend; p1++) {
  86.         if (Post_ss AND *p1 EQ Post_ss_delim) {
  87.             /* IF NO CHARACTERS BETWEEN COMMAND DELIMITERS, LEAVE FOR
  88.             LATER SUBSTITUTION OF A SINGLE COMMAND DELIMITER TO BE
  89.             PRINTED. */
  90.             if (*p1+1 EQ Post_ss_delim) {
  91.                 Wordlen -= 1;
  92.                 p1++; /* IGNORE SECOND COMMAND DELIMITER IN THE PRESENT
  93.                     FOR LOOP */
  94.                 continue;
  95.             }
  96.             else {
  97.  
  98.                 /* FIND CLOSING DELIMITER */
  99.                 for (p2 = p1+1; *p2 NE Post_ss_delim; p2++)
  100.                     ;
  101.                 
  102.                 /* CORRECT Wordlen */
  103.                 Wordlen -= (p2 - p1 + 1);
  104.  
  105.                 /* CONTINUE SCANNING AFTER THE TERMINAL DELIMITER */
  106.                 p1 = p2; /* for loop will now p1++ */
  107.             }
  108.         }
  109.         else if (*p1 EQ BACKSPACE) Wordlen -= 2;
  110.     }
  111. #ifdef DEBUG
  112.     if (Debug) {
  113.         fprintf(STDERR,"Getwrd: \"");
  114.         for (p1 = Wordbegin; p1 <= Wordend; p1++) putc(*p1,STDERR);
  115.         fprintf(STDERR,"\" (Wordlen = %d)\n",Wordlen);
  116.     }
  117. #endif
  118.     return(1);
  119. }
  120.  
  121. /*-------------------------------------------------------------------------*/
  122. getspaces(next)
  123.  
  124. /* DETERMINES THE VALUE OF Spacecnt, WHICH IS THE NUMBER OF SPACES WHICH
  125. SHOULD SEPARATE THE PREVIOUS WORD FROM THE NEXT WORD. NOTE THAT EXPANSION OF
  126. NON-LEADING TABS IS NOT APPLICABLE WHEN FILLING, i. e. IN THIS FUNCTION */
  127.  
  128.     char **next;
  129.     {
  130.     Spacecnt = 0;
  131.     while (**next EQ SPACE OR
  132.             **next EQ TAB OR
  133.             **next EQ NULL) {
  134.         Spacecnt++; /* formerly Spacecnt=1 */
  135.         if (**next NE NULL) (*next)++;
  136.         else break;
  137.     }
  138. /*
  139.     /* PUT 2 SPACES AFTER PERIOD ENDING SENTENCE IF
  140.         1. NEXT WORD IS CAPITALIZED
  141.         2. PRECEDING WORD IS LONGER THAN 2 LETTERS (FOR INITIALS, Wm., Dr.)
  142.     */
  143.     if (*Wordend EQ '.' AND (Wordlen > 3)
  144.     AND Spacecnt AND (isupper(**next)))
  145.         Spacecnt = 2;
  146. */
  147.     /* IF THE PREVIOUS WORD ENDS IN A DASH AT THE END OF THE LINE, THEN WE
  148.     ARE IN THE MIDDLE OF A HYPHENATED WORD AND NO SPACES SHOULD FOLLOW THE
  149.     DASH. */
  150.  
  151.     if (*Wordend EQ DASH
  152.         AND ((*next - Wordend) EQ 1)
  153.         AND (**next EQ NULL)) Spacecnt = 0;
  154. #ifdef DEBUG
  155.     if (Debug) fprintf(STDERR,"Getspaces: Spacecnt = %d\n",Spacecnt);
  156. #endif
  157. }
  158.  
  159. /*-------------------------------------------------------------------------*/
  160. putwrd() {
  161.     int newlen, line_len, indelim;
  162.     char *p1;
  163. #ifdef DEBUG
  164.     if (Debug) fprintf(STDERR,"putwrd(entry)");
  165. #endif
  166.     line_len = Rmval - Tival;
  167.     newlen = Outlen + Wordlen + Spacecnt;
  168.     if (newlen > line_len) {
  169.         if (!Sanders AND Justify) {
  170.             spread (Outbuf, line_len - Outlen + 1, Outwrds);
  171.         }
  172.         if (Sanders AND Justify) brk(FORCEPRINT);
  173.         else brk(CR);
  174.     }
  175.     if (Wordlen) Outwrds++;
  176.     for (p1 = Wordbegin; p1 <= Wordend; p1++) {
  177. #ifdef DEBUG
  178.         if (Debug) putc(*p1,STDERR);
  179. #endif
  180.         *(Nextout++) = *p1;
  181.     }
  182.     if (Spacecnt < 0 OR Wordlen < 0) {
  183.         fprintf(STDERR,
  184.             "Wordlen=%d, Spacecnt=%d, EXIT IN getwrd()\n",
  185.             Wordlen,Spacecnt);
  186.         exit(0);
  187.     }
  188.     fillbuf(&Nextout, SPACE, Spacecnt);
  189.     Outlen += Wordlen + Spacecnt;
  190. #ifdef DEBUG
  191.     if (Debug) fprintf(STDERR,
  192.         "(return)Outwrds=%d  Outlen=%d\n",Outwrds,Outlen);
  193. #endif
  194. }
  195. /*-------------------------------------------------------------------------*/
  196. fillbuf(next, c, cnt)
  197.     char c, **next;
  198.     int cnt;
  199.     {
  200.     while (cnt--) {
  201.         **next = c;
  202.         *next += 1;
  203.     }
  204. }
  205. /*-------------------------------------------------------------------------*/
  206. fillulbuf(textline)
  207.     char *textline;
  208.     {
  209.     int indelim;
  210.     char *p, *nextul, *pflip;
  211. #ifdef DEBUG
  212.     if (Debug) fprintf(STDERR,"Fillulbuf\n");
  213. #endif
  214.     pflip = getflip('U', Ul_plus, textline);
  215.     if (!pflip) {
  216.         *Ulbuf = NULL;
  217.         return(0);
  218.     }
  219.     nextul = Ulbuf;
  220.     indelim = NO;
  221.     for(p = textline; *p; p++) {
  222.         /* SET Ul_plus */
  223.         if (p EQ pflip) {
  224.             Ul_plus? Ul_plus=NO: Ul_plus=YES;
  225.             pflip = getflip('U', Ul_plus, pflip+1);
  226.         }
  227.         /* SKIP POST SUBSTITUTIONS */
  228.         if (Post_ss AND *p EQ Post_ss_delim) {
  229.             if (indelim) {
  230.                 indelim = NO;
  231.                 continue;
  232.             }
  233.             else indelim = YES;
  234.         }
  235.         if (indelim) continue;
  236.         
  237.         if (*p EQ BACKSPACE) {
  238.             nextul--;
  239.             continue;
  240.         }
  241.         if (Ul_plus AND (isalpha(*p) OR isdigit(*p)))
  242.             *nextul++ = '_';
  243.         else *nextul++ = SPACE;
  244.     }
  245.     *nextul = NULL;
  246. }
  247. /*-------------------------------------------------------------------------*/
  248. boldonly(textline)
  249.     char *textline;
  250.     {
  251.     int indelim;
  252.     char *p, *nextul, *pflip;
  253. #ifdef DEBUG
  254.     if (Debug) fprintf(STDERR,"BOLDONLY\n");
  255. #endif
  256.     pflip = getflip('B', Bo_plus, textline);
  257.     if (!pflip) return(NO);
  258.     indelim = NO;
  259.     for(p = textline; *p; p++) {
  260.         /* SET Bo_plus */
  261.         if (p EQ pflip) {
  262.             Bo_plus? Bo_plus=NO: Bo_plus=YES;
  263.             pflip = getflip('B', Bo_plus, pflip+1);
  264.         }
  265.         /* SKIP POST SUBSTITUTIONS */
  266.         if (Post_ss AND *p EQ Post_ss_delim) {
  267.             if (indelim) {
  268.                 indelim = NO;
  269.                 continue;
  270.             }
  271.             else indelim = YES;
  272.         }
  273.         if (indelim OR
  274.             (*p EQ BACKSPACE) OR
  275.             Bo_plus
  276.             ) continue;
  277.         else *p = SPACE;
  278.     }
  279.     return(YES);
  280. }
  281. /*-------------------------------------------------------------------------*/
  282. getflip(type, status, previous)
  283.     char type, *previous;
  284.     int status;
  285.     {
  286.     char flip[16];
  287.     char